package com.lungle.warmdiary.app.ui.guide;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lungle.warmdiary.R;
import com.lungle.warmdiary.app.ui.ImagePreview2;

/**
 * Created by zhangxueyuan on 16/9/21.
 * <p>
 * 图文引导页面layout
 */
public class TextImageLayout extends LinearLayout {


    private ImageView close;
    private ImageView guideImage;
    private View textImage;
    public TextView tv;
    public View closeLayout;
    private ViewTreeObserver.OnPreDrawListener onPreDrawListener;


    public TextImageLayout(Context context, AttributeSet attrs) {
        super(context, attrs);

        View view = View.inflate(context, R.layout.view_guide_layout, this);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.guideView);
        String titleValue = typedArray.getString(R.styleable.guideView_guidetitle);
        Drawable guideImageValue = typedArray.getDrawable(R.styleable.guideView_guideImage);
        //px
        int guideImageHeightValue = typedArray.getDimensionPixelSize(R.styleable.guideView_guideImageHeight, 0);
        boolean closeOff = typedArray.getBoolean(R.styleable.guideView_closeoff, true);


        guideImage = (ImageView) view.findViewById(R.id.vl_guide_image);
        if (guideImageValue != null) {
            if (guideImageHeightValue > 0) {
                android.view.ViewGroup.LayoutParams para = guideImage.getLayoutParams();
                para.height = guideImageHeightValue;
                guideImage.setLayoutParams(para);
            }
            guideImage.setImageDrawable(guideImageValue);

            guideImage.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    ImagePreview2 imagePreview2= new ImagePreview2(context);
                    imagePreview2.showPreview(v,guideImageValue);
                }
            });
        }

        tv = (TextView) view.findViewById(R.id.vl_guide_text);
        if (!TextUtils.isEmpty(titleValue)) {
            tv.setText(titleValue);
            //tv.getLineCount() 绘画后才知道！！
//            if (tv.getLineCount() > 1) {
//                tv.setGravity(Gravity.START);
//            }
            ViewTreeObserver vto = tv.getViewTreeObserver();
            ViewTreeObserver.OnPreDrawListener onPreDrawListener =
                    new ViewTreeObserver.OnPreDrawListener() {
                        @Override
                        public boolean onPreDraw() {
                            if (tv.getLineCount() > 1) {
                                tv.setGravity(Gravity.START);
                            }
                            return true;
                        }
                    };
            vto.addOnPreDrawListener(onPreDrawListener);
        }


        closeLayout = view.findViewById(R.id.vl_close_layout);
        if (closeOff)

        {
            closeLayout.setVisibility(GONE);
        } else

        {
//            view.findViewById(R.id.vl_guide_all).getBackground().setAlpha(204);

            textImage = view.findViewById(R.id.vl_text_image);
            textImage.setBackgroundResource(R.drawable.list_item_background);
            closeLayout.setVisibility(VISIBLE);
            closeLayout.setBackgroundColor(getResources().getColor(R.color.black));
            closeLayout.getBackground().setAlpha(204);

            close = (ImageView) view.findViewById(R.id.guide_close);
            close.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    TextImageLayout.this.setVisibility(GONE);
                }
            });
        }

        typedArray.recycle();
    }

    public int dpToPixel(int dp) {
        DisplayMetrics displaymetrics = new DisplayMetrics();
        return dp * (displaymetrics.densityDpi / 160);
    }

    public void setGuideImage(int rid) {
        guideImage.setImageResource(rid);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_MOVE:
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_UP:
                return true;
        }
        return false;
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (onPreDrawListener != null && tv != null) {
            tv.getViewTreeObserver().removeOnPreDrawListener(onPreDrawListener);
        }
    }


}
